//
//  MoneyRefundCtr.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/9.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit


public enum GoodsReturnPayStatus:String{
    case 退款申请中 = "80"
    case 退款驳回 = "81"
    case 退款申请已取消 = "82"
    case 同意退款 = "83"
    case 退款成功 = "84" //退款到账
    case none = ""
    
    
    public func doAction(with btName:String,cell:JoCellNode?,at controller:UIViewController?,order:NSMutableDictionary) {
        switch btName {
        case "请确认退货物流".loc:
            let ctr = ConfirmSendGoodsCtr()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        default:
            break
        }
        
    }
    
    public func noteText(obj:NSMutableDictionary)->(text:NSAttributedString,backgroundColor:UIColor,show:Bool){
        
        switch self {
        case .同意退款:
            let text = "寄送地址：云南省·昆明市·西山区 海埂路前卫记到100号\n".ui.font13.cl_fff.lineSpace(5).astring
            return (text,___shop.tint,true)
        default:
            let text = "".ui.lfont13.cl_fff.lineSpace(5).astring
            return (text,___shop.second,false)
        }
        
    }
    
    public var barInfo:(name:String,color:UIColor,icon:String,info:String,bt:String){
        get{
            
            let able_hex = ___shop.tint
            let disable_hex = UIColor(shex: "#aaa")
            
            let default_icon = "wddd_pingjia"
            switch self {
            case .退款申请中:
                return ("退款申请中".loc,able_hex,default_icon,"","")
            case .退款驳回:
                return ("退款驳回".loc,able_hex,default_icon,"","")
            case .退款申请已取消:
                return ("已取消".loc,disable_hex,default_icon,"","")
            case .同意退款:
                return ("卖家同意退货".loc,able_hex,default_icon,"","请确认退货物流".loc)
            case .退款成功:
                return ("正在寄送退货物流".loc,able_hex,default_icon,"您已签收购买的商品","去评价".loc)
            case .none:
                return ("",able_hex,default_icon,"","")
            }
        }
    }
    
    public func limitTime(obj:NSMutableDictionary)->Date?{
        switch self {
        case .退款申请中,.同意退款:
            return Date()
        default:
            return nil
        }
    }
    
    public func statusSubtitle(obj:NSMutableDictionary) -> NSAttributedString {
        switch self {
        case .退款申请中:
            return "等待卖家处理,剩余10天5时4分".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退款申请已取消:
            return "您已取消退货申请".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .同意退款:
            return "卖家同意退款，平台会在48小时将购买款项返回到您的支付账号".loc.ui.font13.lineSpace(5).cl_fff.astring
        default:
            return "".ui.font12.cl_fff.lineSpace(5).astring
        }
    }
}

open class MoneyRefundCtr: ASListController{
    
    open var sections:TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cellID":"status","status":"83","__model":mo.model],
                    ["cellID":"note"],
                    ["cellID":"history","title":"协商历史".loc],
                    ],
                [
                    ["cellID":"blank","height":"8","hex":UIColor.table_bg_hex],
                    ["cellID":"title","title":"退款信息".loc],
                    ["cellID":"goods","__model":mo.model],
                    ["cellID":"apply"],
                    ],
                [
                ]
            ]
            return sec
        }
    }
    
    public var selector:TP.selector = [
        "cellID<status>":StatusCell.self,
        "cellID<actions>":Goods.BtsStatusCell.self,
        "cellID<note>":SaleAfter.NoteCell.self,
        "cellID<history>":SaleAfter.HistoryCell.self,
        "cellID<blank>":BlankNode.self,
        "cellID<goods>":SaleAfter.GoodsCell.self,
        "cellID<title>":SaleAfter.TitleCell.self,
        "cellID<apply>":SaleAfter.ApplayInfoCell.self,
        "cellID<物流>":SaleAfter.LogisticsCell.self,
        ]
    
    
     open var returnID:String{
        get{
            return ""
        }
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        title = "退货信息".loc
        mo.detail = mo.model
        tableNode.backgroundColor = UIColor.table_bg_color
    }
    
    
    //    override func mo_request_send() {
    //
    //        weak var wself = self
    //        loadingV.loading()
    //        JoTask().get.log.params(["orderid":returnID])._url(.订单详情).log_response().json_handle { (success, msg, obj, resp) in
    //            wself?.loadingV.dismiss()
    //            if success{
    //                if let dict = obj as? NSMutableDictionary{
    //                    wself?.mo.detail = dict
    //                    return
    //                }
    //            }
    //            wself?.mo_request_alert(msg: msg, title: "", bt_title: "重试")
    //        }.run()
    //    }
    
    open override func mo_detail_reload() {
        tableNode.load(sections: sections, selector: selector)
        tableNode.reloadData()
        //        let msgid = MsgMap.getMsgID(type: .order, objID: self.orderID)
        
    }
    
    
}

extension MoneyRefundCtr{
    
     class StatusCell: JoCellNode {
        let nameL = JoTextNode()
        let infoL = JoTextNode()
        let timeL = JoTextNode()
        var status:GoodsReturnPayStatus = .none
        
        let bt = ASButtonNode()
        
        
        let noteL = JoTextNode()
        override func mo_model_reload() {
            backgroundColor = ._background
            
            let order = mo.model
            self.separatorColor = UIColor.separator_color
            self.separatorHeight = TP.separatorHeight
            
            self.status = GoodsReturnPayStatus(rawValue: order["status",""]) ?? .none
            let info = status.barInfo
            nameL.attributedText = info.name.ui.bfont16.cl_fff.astring
            infoL.attributedText = status.statusSubtitle(obj: order)
            timeL.attributedText = "剩余10天5时4分".ui.font12.cl_fff.astring
            
            let noteInfo = status.noteText(obj: mo.model)
            noteL.attributedText = noteInfo.text
            noteBk.backgroundColor =  noteInfo.backgroundColor
            
            colorBk.backgroundColor = info.color
            
            if info.bt.len > 0
            {
                bt.ui.font(size: 12).ncolor(hex: "#fff").padding(insets: [5,7]).ntitle(title: info.bt).set(radius: 2, width: 1, color: .white)
                bt.addTarget(self, action: #selector(doAction(sender:)), forControlEvents: .touchUpInside)
            }
            else{
                bt.alpha = 0
            }
        }
        
        @objc func doAction(sender:ASButtonNode) {
            weak var wself = self
            var model = mo.model
            
            if let __model = DataNode<NSMutableDictionary>.path(node: "__model", mo.model)
            {
                
                model = __model
            }
            
            self.status.doAction(with: sender.actionID, cell: wself, at: content_controller, order: model)
            
        }
        
        let colorBk = ASDisplayNode()
        let noteBk = ASDisplayNode()
        
        
        override func specGetter() -> _Spec {
            var specs:[_Spec] = [_Spec.stack(["h|10",&-.center,_justfy(.space_between)], childs: [
                nameL.Spec,bt.Spec
                ]),infoL.Spec]
            if let _ = status.limitTime(obj: mo.model)
            {
                specs.append(timeL.Spec)
            }
            let spec = _Spec.stack(["v|8",_justfy(.space_between),[20,AppTheme.edge],&-.stretch], childs: specs)
            let content = _Spec.back([], over: spec, back: colorBk.Spec)
            var root_childs:[_Spec] = [content]
            if status.noteText(obj: mo.model).show{
                let note:_Spec = .inset([], [10,AppTheme.edge], child: noteL.Spec)
                root_childs.append(.back([], over: note, back: noteBk.Spec))
            }
            
            let root = _Spec.stack(["v",&-.stretch], childs: root_childs)
            return root
        }
    }
}

